<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/register.css">
</head>

<body>
    <div class="container">
        <div class="row">   
            <div class="col-sm">

            </div>

            <div class="col-sm">

                <div class="col">
                    <div class="alert alert-danger alert-dismissible fade show invisible text-center"
                        style="width: 35rem;" role="alert">
                        <strong>提示：</strong><label for="" id="msg">555</label>
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                </div>

                <!-- 注册 -->
                <div class="shadow-lg p-3 mb-5 rounded" style="background-color:rgba(0,0,0,0);" >
                    <div class="col-sm" style="background-color:rgba(0,0,0,0);">
                        <div class="card" style="background-color: transparent;">
                            <div class="card-header" style="color: red;font-size: 20px;">
                                注册
                            </div>
                            <div class="card-body">
                                <form id="registerForm">
                                    <div class="form-group">
                                        <label for="exampleInputEmail1" style="color: red;font-size: 20px;">用户名：</label>
                                        <input type="" class="form-control" id="exampleInputEmail1"
                                            aria-describedby="emailHelp" name="username" style="background-color:rgba(0,0,0,0);">
                                    </div>
                                    <div class="form-group">
                                        <label for="exampleInputPassword1" style="color:violet;font-size: 20px;">密码：</label>
                                        <input type="password" class="form-control" id="exampleInputPassword1"
                                            name="password" style="background-color:rgba(0,0,0,0);">
                                    </div>
                                    <div class="form-group">
                                        <label for="exampleInputPassword1" style="color: red;font-size: 20px;">确认密码：</label>
                                        <input type="password" class="form-control" id="exampleInputPassword2"
                                            name="confirmpassword" style="background-color:rgba(0,0,0,0);">
                                    </div>
                                    <div>
                                        <button type="button" class="btn btn-primary"
                                            onclick="zc()">注&nbsp;&nbsp;&nbsp;册</button>
                                        <button type="button" class="btn btn-primary"
                                            onclick="qx()">取&nbsp;&nbsp;&nbsp;消</button>
                                    </div>
    
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="col-sm">

            </div>




            


        </div>
    </div>
    <script src="js/jquery-3.6.0.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script>
        //点击注册时 执行的方法（获取数据，进行初步验证，验证通过则通过ajax向后台提交数据；否则提示验证不通过的信息）
        function zc() {
            let user = $('#exampleInputEmail1').val();
            let password = $('#exampleInputPassword1').val();
            let confirmpassword = $('#exampleInputPassword2').val();
            console.log(user);
            console.log(password);
            console.log(confirmpassword);
            if (user !== '' && password !== '' && confirmpassword === password) {
                //ajax请求
                $.ajax({
                    url: "/registerDone",
                    type: "POST",
                    data: $('#registerForm').serialize(),
                    dataType: "json",
                    success: function (res) {
                        console.log(res);
                        if (res.code === 200) {
                            window.location.href = "/login";
                        } else {
                            alert_fn(res.msg);
                        }
                    },
                    error: function (msg) {
                        alert_fn('网络服务器问题');
                    }
                })
            } else if (user === '' && password === '') {
                alert('用户名或密码不能为空');
            } else if (confirmpassword === '') {
                alert('确认密码不能为空');
            } else {
                alert("密码和确认密码不一致");
            }
        }

        function alert_fn(msg) {
            msg = msg || '密码不能为空，两次密码应该一致';
            // 替换提示的内容
            $('#msg').text(msg);
            // 移除不显示的class，则alert将显示
            $('.invisible').removeClass('invisible');
            // 三秒中后，给alert增加不显示的class，则alert重新隐藏
            setTimeout(() => {
                $('.alert').addClass('invisible');
            }, 3000);
        }


        function qx() {
            window.location.href = '/login';
        }


    </script>
</body>

</html>